/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gmGUI.java
 *
 * Created on Dec 5, 2009, 3:40:46 PM
 */

package ks.client.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.JButton;

import ks.client.UserContext;
import ks.client.controllers.ChangeStatusController;
import ks.client.controllers.KickPlayerClick;
import ks.client.controllers.LeaveTableClick;
import ks.client.controllers.ProcessLobbyInputController;
import ks.client.controllers.TableOptionsClick;
import ks.client.controllers.game.ProcessTableInputController;
import ks.client.controllers.game.AcceptPlayerController;
import ks.client.controllers.game.RejectPlayerController;
import ks.client.controllers.game.TableLeaveController;
import ks.client.designfactory.PlayerRanking;
import ks.client.lobby.Lobby;
import ks.client.lobby.RoomGUI;
import ks.common.view.Container;
import ks.managers.table.Table;
import ks.managers.table.TableManager;

/**
 * 
 * @author szutshi
 */
public class GMGUI extends javax.swing.JFrame {
	Lobby lobby;
	int num;
	UserContext uc = null;
	TableManager tm = null;
	Table t = null;

	/** Creates new form gmGUI */
	public GMGUI(Lobby lobby, int num) {
		this.lobby = lobby;
		this.num = num;
		uc = lobby.getUserContext();
		tm = uc.getTableManager();
		t = tm.getTable(num);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jPanel2 = new PlayerRanking();
		jScrollPane1 = new javax.swing.JScrollPane();
		jList1 = new javax.swing.JList();
		jTextField1 = getTableInput();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTextArea1 = getTableOutput();
		JButton jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jButton4 = new javax.swing.JButton();
		jButton5 = new javax.swing.JButton();
		jButton6 = new javax.swing.JButton();
		jButton7 = new javax.swing.JButton();
		JButton jButton8 = new javax.swing.JButton();

		jPanel2.setPreferredSize(new Dimension(100, 100));

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setBackground(new java.awt.Color(0, 0, 0));

		jButton8.setText("Table Options");
		jButton8.addActionListener(new TableOptionsClick(lobby, num));

		jPanel1.setBackground(new java.awt.Color(204, 0, 51));
		jPanel1.setForeground(new java.awt.Color(255, 255, 255));

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 677,
				Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 381,
				Short.MAX_VALUE));

		jPanel2.setBackground(new java.awt.Color(204, 0, 51));
		jPanel2.setForeground(new java.awt.Color(255, 255, 255));

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 181,
				Short.MAX_VALUE));
		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 132,
				Short.MAX_VALUE));

		jList1.setModel(new javax.swing.AbstractListModel() {
			String[] strings = {};

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jScrollPane1.setViewportView(jList1);

		jButton1.setText("Start Game");

		UserContext uc = lobby.getUserContext();
		TableManager tm = uc.getTableManager();
		Table t = tm.getTable(num);
		String mod = t.getModerator();
		String me = uc.getUser();
		jButton2.setText("Accept Player");
		jButton2.setEnabled(false);
		jButton2.setVisible(false);

		jButton3.setText("Reject Player");
		jButton3.setEnabled(false);
		jButton3.setVisible(false);
		
		jButton4.setText("Kick Player");
		jButton4.addActionListener(new KickPlayerClick(lobby, num));
		jButton4.setEnabled(false);
		jButton4.setVisible(false);
		
		jButton5.setText("New Hand");
		jButton5.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton5ActionPerformed(evt);
			}
		});

		jButton6.setText("Restart Hand");
		jButton6.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton6ActionPerformed(evt);
			}
		});

		jButton7.setText("Leave Table");
		jButton7.addActionListener(new LeaveTableClick(lobby, uc, num));

		jScrollPane2.setViewportView(jTextArea1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jPanel1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane2,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								281,
																								Short.MAX_VALUE)
																						.addComponent(
																								jTextField1,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								281,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				87,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								jButton1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								110,
																								Short.MAX_VALUE)
																						.addComponent(
																								jButton2,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jButton3,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jButton4,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				jPanel2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jButton5)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				jButton6)
																		.addGap(
																				81,
																				81,
																				81)
																		.addComponent(
																				jButton8,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				187,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				130,
																				Short.MAX_VALUE)
																		.addComponent(
																				jButton7)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(20, 20, 20)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButton5)
														.addComponent(jButton6)
														.addComponent(jButton7)
														.addComponent(jButton8))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jPanel1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				10,
																				10,
																				10)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addComponent(
																												jScrollPane2,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												102,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												jTextField1,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																										.addComponent(
																												jScrollPane1,
																												javax.swing.GroupLayout.Alignment.TRAILING,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												128,
																												Short.MAX_VALUE)
																										.addGroup(
																												javax.swing.GroupLayout.Alignment.TRAILING,
																												layout
																														.createSequentialGroup()
																														.addComponent(
																																jButton1)
																														.addPreferredGap(
																																javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																														.addComponent(
																																jButton2)
																														.addPreferredGap(
																																javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																														.addComponent(
																																jButton3)
																														.addPreferredGap(
																																javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																														.addComponent(
																																jButton4)
																														.addGap(
																																15,
																																15,
																																15)))))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jPanel2,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)))
										.addGap(24, 24, 24)));
		updateUserList();
		pack();
	}// </editor-fold>

	private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
	}

	private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
		new LeaveTableClick(lobby, lobby.getUserContext(), num);
	}

	public JTextArea getTableOutput() {
		if (jTextArea1 == null) {
			jTextArea1 = new JTextArea();
			jTextArea1.setColumns(20);
			jTextArea1.setRows(5);
		}
		return jTextArea1;
	}

	JTextField getTableInput() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField(36);
			// jTextField1.setBounds(11, 650, 581, 24);
			jTextField1.setPreferredSize(new Dimension(50, 24));

			jTextField1.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					String s = jTextField1.getText();
					jTextField1.setText("");

					// hand off to the appropriate controller.
					new ProcessTableInputController(lobby.getUserContext(), num)
							.process(s);
				}

			});
		}
		return jTextField1;
	}

	/**
	 * Append text to the lobby.
	 * 
	 * @param s
	 */
	public void append(String s) {
		String t = jTextArea1.getText() + "\n" + s;
		jTextArea1.setText(t);
		scrollToEnd();
	}

	/**
	 * Make sure we can scroll to the end on demand.
	 */
	void scrollToEnd() {
		jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
	}

	public void updateUserList() {
		DefaultListModel model = new DefaultListModel();

		TableManager tm = lobby.getUserContext().getTableManager();
		Table table = tm.getTable(num);

		Collection<String> players = table.getPlayers();

		int numPlayers = 0;
		for (String player : players) {
			if (player != null)
				numPlayers++;
		}

		model.setSize(numPlayers);

		int atItem = 0;
		for (String player : players) {
			if (player != null) {
				model.add(atItem, player);
				atItem++;
			}
		}

		jList1.setModel(model);
		jList1.revalidate();

		String mod = table.getModerator();

		boolean visibility = false;
		UserContext uc = lobby.getUserContext();
		String me = uc.getUser();

		
		if (me.equalsIgnoreCase(table.getModerator())) {
			System.out.println("Moderator exists");
			visibility = true;

			if (jButton2 != null) {
				jButton2.setVisible(visibility);
				jButton2.setEnabled(visibility);
				jButton2.revalidate();
				jButton2.repaint();
			}
			if (jButton3 != null) {
				jButton3.setVisible(visibility);
				jButton3.setEnabled(visibility);
				jButton3.revalidate();
				jButton3.repaint();
			}
			if (jButton4 != null) {
				jButton4.setVisible(visibility);
				jButton4.setEnabled(visibility);
				jButton4.revalidate();
				jButton4.repaint();
			}

			this.repaint();
		}
	}

	public JList getList() {
		return jList1;
	}

	/**
	 * @param args
	 *            the command line arguments
	 */

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JButton jButton5;
	private javax.swing.JButton jButton6;
	private javax.swing.JButton jButton7;
	private JButton jButton8;
	private javax.swing.JList jList1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextField jTextField1;
	// End of variables declaration

}
